<?php

namespace Home\Controller;

class ShitTimeController extends CommonController {

    public function getRecentShitTime() {
        if (!isset($_GET['user_id'])) {
            return $this->ajaxReturn($this->buildResult(false, 'user_id必填'));
        }

        $sevenDays = 84600 * 7;
        $ShitTimeService = D('ShitTime', 'Service');
        $shitTime = $ShitTimeService->getRecentShitTime($_GET['user_id'], date('Y-m-d H:i:s', time() - $sevenDays), date('Y-m-d H:i:s', time()), 7);

        $result = array(
            'status' => true,
            'msg' => '成功得到七天内的记录',
            'date' => $shitTime['dateKeys'],
            'time' => $shitTime['durations']
        );

        return $this->ajaxReturn($result);
    }

    public function create() {
        if (!isset($_POST['req'])) {
            return $this->ajaxReturn($this->buildResult(false, 'req必填'));
        }

        $ShitTime = D('ShitTime');
        $shitTime = json_decode($_POST['req'], true);
        if ($shitTime = $ShitTime->create($shitTime)) {
            if (false === $ShitTime->add($shitTime)) {
                return $this->ajaxReturn($this->buildResult(false, $ShitTime->getDbError()));
            }
        }

        return $this->ajaxReturn($this->buildResult(true, '记录拉屎时间成功'));
    }
}
